//
//  BDHUD.m
//  ProjectforHDX
//
//  Created by HDX on 16/7/19.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "BDHUD.h"

@interface BDHUD ()

+(instancetype)sharedManager;
@property (nonatomic, strong) UIView *overlayView;
@end



@implementation BDHUD

static BDHUD *instance;

+ (instancetype)sharedManager
{
    if (!instance) {
        instance = [[BDHUD alloc] init];
    }
    return instance;
    
   
}
/**
 *  展示HUD
 *
 *  @param UIView 父容器
 *  @param title  文本
 */
-(void)showBDHUD:(UIView*)superView title:(NSString*)title{
    [self dismissHUD];
    if(![title isKindOfClass:[NSString class]]){
        title=[THMangerTool language:@"HUD.title"];
    }
    if(![superView isKindOfClass:[UIView class]]){
        superView=[UIApplication sharedApplication].keyWindow;
        
    }
    
    self.overlayView=superView;
    
   MBProgressHUD * loadView = [MBProgressHUD showHUDAddedTo:superView animated:YES];
    loadView.tag=20160618;
    loadView.label.text = title;
    loadView.mode = MBProgressHUDModeIndeterminate;
}
/**
 *  展示上传进度HUD
 *
 *  @param UIView 父容器
 *  @param title  文本
 */
-(void)showBDUploadHUD:(UIView*)superView title:(NSString*)title{
        [self dismissHUD];
    if(![superView isKindOfClass:[UIView class]]){
        superView=[UIApplication sharedApplication].keyWindow;
        
    }
    if(![title isKindOfClass:[NSString class]]){
        title=[THMangerTool language:@"HUD.upload"];
    }
    self.overlayView=superView;
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc]initWithView:self.overlayView];
    [self.overlayView addSubview:HUD];
    HUD.tag=20160618;
    HUD.mode = MBProgressHUDModeDeterminate;
    HUD.progress=0.05;
    HUD.label.text = title;
    HUD.square = YES;
    [HUD showAnimated:YES];
    
}

/**
 *  修改上传进度HUD
 *
 
 *  @param title  progress
 */
-(void)setBDUploadProgressHUD:(CGFloat)progress{
    MBProgressHUD *view=(MBProgressHUD*)[self.overlayView viewWithTag:20160618];
    if(view){
        view.progress=progress;
    }
}


-(void)dismissHUD{
    BOOL isOK=YES;
    while (isOK) {
 
        MBProgressHUD *view=(MBProgressHUD*)[self.overlayView viewWithTag:20160618];
        if(view){
           [view removeFromSuperview ];
        }
        else{
            isOK=NO;
        }
    }

 
}

-(void)dismissHUD:(UIView*)view{
    BOOL isOK=YES;
    while (isOK) {
        MBProgressHUD *view=(MBProgressHUD*)[[UIApplication sharedApplication].keyWindow viewWithTag:20160618];
        if(view){
            [view removeFromSuperview ];
        }
        else{
            isOK=NO;
        }
    }
    
    
}

/**
 *  提示文本
 */
-(void)showWords:(NSString*)title{
   
    if(![title isKindOfClass:[NSString class]]){
        title=[THMangerTool language:@"HUD.nothing"];
    }
    [APPDELEGATEKEYWINDOW makeToast:title
                           duration:CLEAR_TIME
                           position:[NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/2.f, [UIScreen mainScreen].bounds.size.height - 160)]];
    
}
/**
 *  无数据提示
 */
-(void)showNOneDataViewHUD:(UIView*)superView  top:(CGFloat)topy title:(NSString*)title onClick:(void (^)())click{
       [self dismissHUD];
    if(![superView isKindOfClass:[UIView class]]){
        superView=[UIApplication sharedApplication].keyWindow;
        
    }
    if(![title isKindOfClass:[NSString class]]){
        title=[THMangerTool language:@"HUD.title"];
    }
    self.overlayView=superView;
 
     UIImage *img=BDFILEIMAGENAEM(@"default/img_noData");
 
   NoneDateView *_noneDateView=[[NoneDateView alloc] initView:self.overlayView setImage:img setImageFream:CGRectMake((self.overlayView.frame.size.width- img.size.width)/2, topy, img.size.width, img.size.height) Title:title];
    
    _noneDateView.tag=20160618;
    
    _noneDateView.selectBlock=^(NSDictionary *dict){
        if(click){
            click();
        }
    };
    for(RZCustomLable *lab in _noneDateView.subviews){
        if([lab isKindOfClass:[RZCustomLable class]]){
            lab.font=CustomFont(13);
            lab.top+=10;
            lab.width=BOUNDSWIDTH-20;
            lab.left=10;
 
        }
    }
    
}

- (void)showViewToView:(UIView *)view ContentType:(NSInteger)type headImage:(UIImage *)img headTitle:(NSString *)title headColor:(UIColor *)color text:(id)content bottons:(NSArray*)btns click:(showDetailsViewBlock)success  {
    //查看外协信息
    THOpenView *openView=[[THOpenView alloc] ShowOpenContentViewFream:[UIScreen mainScreen].bounds];
    openView.tag=20160618;
    openView.backgroundColor=[UtilCheck hexStringToColor:@"000000" alpha:0.23];
    
   
    


   showDetailsView *showView =[[showDetailsView alloc] initWithFrameContentType:0 headImage:img headTitle:title headColor:color text:content click:^(NSInteger index, id responseObject) {
        
        success(index,responseObject);
        [openView dismiss];
        
    } bottons:btns];
    
    showView.center =[UIApplication sharedApplication].keyWindow.center;
    [openView addSubview:showView];//主要
    [[UIApplication sharedApplication].keyWindow addSubview:openView];//主要
    openView.OpenViewObjct= showView;

    
}

/**
 *  发货信息
 *
 *  @param view     <#view description#>
 *  @param img      <#img description#>
 *  @param title    <#title description#>
 *  @param subTitle <#subTitle description#>
 *  @param addr     <#addr description#>
 *  @param color    <#color description#>
 *  @param array    <#array description#>
 *  @param btns     <#btns description#>
 *  @param success  <#success description#>
 */
- (void)showViewToView:(UIView *)view  HeadImage:(UIImage*)img headTitle:(NSString*)title contacts:(NSString*)subTitle address:(NSString*)addr headColor:(UIColor*)color text:(id)array   bottons:(NSArray*)btns click:(showDetailsViewBlock)success{
    //查看外协信息
    THOpenView *openView=[[THOpenView alloc] ShowOpenContentViewFream:[UIScreen mainScreen].bounds];
    openView.backgroundColor=[UtilCheck hexStringToColor:@"000000" alpha:0.23];
    
   
    
    showDetailsView *showView =[[showDetailsView alloc] initWithHeadImage:img headTitle:title contacts:subTitle address:addr headColor:color text:array click:^(NSInteger index, id responseObject) {
        success(index,responseObject);
        [openView dismiss];
    } bottons:btns ];
 
    
    showView.center =[UIApplication sharedApplication].keyWindow.center;
    [openView addSubview:showView];//主要
    [[UIApplication sharedApplication].keyWindow addSubview:openView];//主要
    openView.OpenViewObjct= showView;
}

/**
 *  故障件
 *
 *  @param view     <#view description#>
 *  @param img      <#img description#>
 *  @param title    <#title description#>
 *  @param subTitle <#subTitle description#>
 *  @param color    <#color description#>
 *  @param array    <#array description#>
 *  @param btns     <#btns description#>
 *  @param success  <#success description#>
 */
- (void)showViewToView:(UIView *)view HeadImage:(UIImage*)img headTitle:(NSString*)title subTitle:(NSString*)subTitle  headColor:(UIColor*)color text:(NSArray*)array    bottons:(NSArray*)btns click:(showDetailsViewBlock)success{
    //查看外协信息
    THOpenView *openView=[[THOpenView alloc] ShowOpenContentViewFream:[UIScreen mainScreen].bounds];
    openView.backgroundColor=[UtilCheck hexStringToColor:@"000000" alpha:0.23];
 
    showDetailsView *showView =[[showDetailsView alloc] initWithHeadImage:img headTitle:title subTitle:subTitle headColor:color text:array click:^(NSInteger index, id responseObject) {
        success(index,responseObject);
        [openView dismiss];
        
    } bottons:btns ];
    
    showView.center =[UIApplication sharedApplication].keyWindow.center;
    [openView addSubview:showView];//主要
    [[UIApplication sharedApplication].keyWindow addSubview:openView];//主要
    openView.OpenViewObjct= showView;
}
@end
